//-----------------------------------------------------------------------
// <copyright file="SqlBase.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Barry Dorrans</author>
// <email></email>
// <date>2004-06-15</date>
// <summary>SQL Base class, containing properties common to all SQL tasks</summary>
//-----------------------------------------------------------------------
#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Microsoft.Sdc.Tasks.Sql
{
    using System;
    using System.Xml;
    using System.Globalization;
    using System.IO;
    using System.Data;
    using System.Data.SqlClient;
    using SdcSql = Microsoft.Sdc.Tasks.Configuration.SqlServer;
    using Microsoft.Build.Framework;

    /// <summary>
    /// This base class provides common properties for all SQL configuration tasks.
    /// </summary>
    /// <remarks>
    /// <para>As this class is an abstract class it cannot be created directly.</para>
    /// </remarks>
    public abstract class SqlBase : TaskBase
    {
        #region Private members

        private string _serverName;
        private string _connectionString;
        private int    _commandTimeout;
        private int    _connectionTimeout;

        #endregion

        /// <summary>
        /// Default Constructor.
        /// </summary>
        protected SqlBase()
        {
            // Default any operation timeout to 30 seconds
            this._commandTimeout = 30;
            // And the connection timeout to 15 seconds
            this._connectionTimeout = 15;
            // Default the connection to point to localhost
            this._serverName = "localhost";


        }

        /// <summary>
        /// SQL Server to perform the specified operation upon.
        /// </summary>
        /// <value>A valid SQL server or instance name</value>
        public string ServerName
        {
            get
            {
                return (this._serverName == null ? String.Empty : this._serverName);
            }
            set
            {
                this._serverName = value;
            }
        }


        /// <summary>
        /// An OLE/DB database connection string
        /// </summary>
        /// <value>A valid OLE/DB connection string used to connect the SQL server to run the operation on.</value>
        /// <remarks>This property can be used instead of, or in addition the combination of 
        /// <see cref="SqlBase.ServerName"/> and
        /// <see cref="ConnectionTimeout"/>.
        /// </remarks>
        public string ConnectionString
        {
            get
            {
                if (this._connectionString == null ||
                    this._connectionString.Length == 0)
                    {
                      return (this._serverName.Length==0 ? String.Empty : BuildConnectionString());
                    }
                    else
                    {
                        return (_connectionString);
                    }
            }
            set
            {
                this._connectionString = value;
            }
        }

        /// <summary>
        /// Gets or sets the wait time in seconds before terminating the 
        /// attempt to execute a command and generating an error.
        /// </summary>
        /// <value>0 (for no timeout), or a positive integer (seconds)</value>
        /// <remarks>
        /// The default value for CommandTimeout is 30 seconds.
        /// An attempt to set a negative value will result in an <exception cref="ArgumentOutOfRangeException" />.
        /// </remarks>
        public int CommandTimeout
        {
            get
            {
                return _commandTimeout;
            }
            set
            {
                if (this._commandTimeout >= 0)
                    this._commandTimeout = value;
                else
                    throw new System.ArgumentOutOfRangeException("CommandTimeout", value, "CommandTimeout values may not be negative");
            }
        }

        /// <summary>
        /// Gets or sets the wait time in seconds before terminating the 
        /// attempt to connect to a server.
        /// </summary>
        /// <value>0 (for no timeout), or a positive integer (seconds)</value>
        /// <remarks>
        /// The default value for ConnectionTimeout is 15 seconds.
        /// An attempt to set a negative value will result in an <exception cref="ArgumentOutOfRangeException" />.
        /// </remarks>
        public int ConnectionTimeout
        {
            get
            {
                return _connectionTimeout;
            }
            set
            {
                if (this._connectionTimeout >= 0)
                    this._connectionTimeout = value;
                else
                    throw new System.ArgumentOutOfRangeException("CommandTimeout", value, "CommandTimeout values may not be negative");
            }
        }

        /// <summary>
        /// Builds a suitable connection string from the exposed, common properties
        /// </summary>
        /// <returns>An OLE/DB connection string</returns>
        internal string BuildConnectionString()
        {
            return string.Format(CultureInfo.InvariantCulture,
                "Data Source={0};Integrated Security=SSPI;Connection Timeout={1};",
                this._serverName, this._connectionTimeout);
        }
    }
}
